﻿using System;
using System.Web;
using System.Xml;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using System.Reflection;
using Framework.Library.Core.Provider.Caches;

namespace Framework.Library.Base.Helper
{
    public class LanguageHelper : BaseClass
    {
        public List<PortalLanguageCom> lstApp = new List<PortalLanguageCom>();

        public static LanguageHelper Language
        {
            get { return new LanguageHelper(); }
        }
        public LanguageHelper()
        {
            //lstApp = Caching.Data.Get<List<PortalLanguageCom>>("LANGUAGE", Configurations.PORTALID + "_" + Configurations.LANGID);
            //if (lstApp == null)
            //{
                lstApp = ManagerFactory.PortalLanguageComManager.GetLanguages(Configurations.PORTALID, Configurations.LANGID);
                if (lstApp == null)
                    lstApp = new List<PortalLanguageCom>();
                //else
                //    Caching.Data.Set("LANGUAGE", lstApp, Configurations.PORTALID + "_" + Configurations.LANGID);
            //}
        }

        private List<PortalLanguageCom> GetLanguages(string page)
        {
            List<PortalLanguageCom> lstLanguages = lstApp.FindAll(x => x.PAGE == page);
            return lstLanguages;
        }
        private LangSetting GetLanguage(string tag)
        {
            PortalLanguageCom info = lstApp.Find(x => x.TAG == tag);
            if (info != null)
                return new LangSetting() { value = info.VALUE, description = info.DESCRIPTION };
            else
                return new LangSetting() { value = string.Format("[{0}]", tag.ToUpper()), description = string.Empty };
        }
        private LangSetting GetLanguage(string page, string tag)
        {
            PortalLanguageCom info = GetLanguages(page).Find(x => x.TAG == tag);
            if (info != null)
                return new LangSetting() { value = info.VALUE, description = info.DESCRIPTION };
            else
                return new LangSetting() { value = string.Format("[{1}.{0}]", tag.ToUpper(), page.ToUpper()), description = string.Empty };
        }
        public string GetText(string tag)
        {
            return GetLanguage(tag).value;
        }
        public string GetText(string page, string tag)
        {
            return GetLanguage(page, tag).value;
        }
        public string GetDescription(string tag)
        {
            return GetLanguage(tag).description;
        }
        public string GetDescription(string tag, string page)
        {
            return GetLanguage(page, tag).description;
        }
    }
    
    [Serializable]
    public class LangSetting
    {
        public string value { get; set; }
        public string description { get; set; }
    }
}